<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Form;
use App\Admin\Grid;
use App\Admin\Grid\Displayers\Actions;
use App\Admin\Layout\Content;
use App\Admin\Traits\ModelForm;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    use ModelForm;

    public function index()
    {
        return \Admin::content(function (Content $content) {
            $content->header('角色');
            $content->description('角色列表');
            $content->body($this->grid()->render());
        });
    }

    protected function grid()
    {
        return Role::grid(function (Grid $grid) {
            $grid->id('编号')->sortable();

            $grid->display_name('角色名')->editable();
            $grid->name('角色标志')->editable();

            $grid->permissions('权限')->pluck('display_name')->label();

            $grid->created_at('添加时间');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('display_name', '角色名');
                $filter->like('name', '角色标志');
                $filter->like('permissions.display_name', '权限');
            });

            $grid->disableExport();
            $grid->disableBatchDelete();
        });
    }

    public function create()
    {
        return \Admin::content(function (Content $content) {
            $content->header('角色');
            $content->description('添加角色');
            $content->row($this->form());
        });
    }

    public function edit($id)
    {
        return \Admin::content(function (Content $content) use ($id) {
            $content->header('角色');
            $content->description('编辑角色');
            $content->row($this->form()->edit($id));
        });
    }

    protected function form()
    {
        return Role::form(function (Form $form) {
            $form->display('id', '编号');

            $form->text('display_name', '角色名');
            $form->text('name', '角色标志');

            $form->checkbox('permissions', '权限')->options(Permission::select('id', 'display_name')->pluck('display_name', 'id')->toArray());

            $form->display('created_at', '添加时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
